﻿Imports System.Data.OleDb

Public Class RecipeDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        img_like.Visible = False



        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim sql = "SELECT * FROM RecipeLikes WHERE [USER] = @f1   AND [Recipe_fkid] = @f2"
        Dim oleDbCmd As New OleDbCommand(sql, oleDbConn)
        oleDbConn.Open()
        oleDbCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))

        Dim dr = oleDbCmd.ExecuteReader()

        If dr.HasRows Then
            img_like.Visible = True
            btn_like.Visible = False
        Else

            img_like.Visible = False
            btn_like.Visible = True
        End If


        oleDbConn.Close()

    End Sub
    Protected Function ShowStars(ByVal numstars As String)
        Dim sb As New System.Text.StringBuilder()
        For index As Integer = 1 To numstars
            sb.Append("*")
        Next
        Return sb
    End Function

    Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewPageEventArgs) Handles DetailsView1.PageIndexChanging

    End Sub


    Protected Sub btn_rating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_rating.Click
        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim delPrevRatings = "DELETE FROM RecipeRating WHERE Rater=@f1 and Recipe_fkid=@f2"
        Dim oleDbCmd2 = New OleDbCommand(delPrevRatings, oleDbConn)
        oleDbCmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbCmd2.Parameters.AddWithValue("@f2", Request.Params("ID"))
        oleDbCmd2.ExecuteNonQuery()


        Dim sql = "INSERT INTO RecipeRating (Rating, Rater, Recipe_fkid) VALUES (@f1,@f2,@f3)"
        Dim oleDbCmd As New OleDbCommand(sql, oleDbConn)
        oleDbCmd.Parameters.AddWithValue("@f1", Rating2.CurrentRating)
        oleDbCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        oleDbCmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        oleDbCmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbCmd.Dispose()
        oleDbConn.Dispose()
    End Sub

   

    Protected Sub btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_like.Click
        img_like.Visible = True
        btn_like.Visible = False


        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim sql = "INSERT INTO RecipeLikes ([Recipe_fkid],[User]) VALUES (@f1,@f2)"
        Dim oleDbCmd As New OleDbCommand(sql, oleDbConn)

        oleDbConn.Open()
        oleDbCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        oleDbCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        oleDbCmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbCmd.Dispose()
        oleDbConn.Dispose()

    End Sub

    Protected Sub btn_Comment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Comment.Click
        Dim sql As String = "INSERT INTO RecipeComments (Author,Title,CommentBody,Recipe_fkid) VALUES (@f1,@f2,@f3,f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_CommentBody.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("RecipeDetails.aspx?ID=" & Request.QueryString("ID"))
    End Sub
End Class